package Day14;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class ATM05 extends JFrame {
    private ArrayList users;
    private User loginUser;
    private String FILE_PATH = "user.dat";
    private Image bgImg = new ImageIcon("Image/background.jpg").getImage();
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private GetPanel getPanel = new GetPanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);
    private SavePanel savePanel = new SavePanel(this);
    private ChangePwdPanel changePwdPanel = new ChangePwdPanel(this);


    public ATM05(){
        this.init();
        this.setContentPane(new JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(bgImg,0,0,this);
            }
        });
        this.setLayout(cardLayout);

        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(getPanel,"get");
        this.add(searchPanel,"search");
        this.add(transferPanel,"transfer");
        this.add(savePanel,"save");
        this.add(changePwdPanel,"changePwd");



        this.setTitle("ATM");
        this.setSize(1024,768);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public void init(){
        users = (ArrayList) fileUtil.readObject(FILE_PATH);
        if(users == null){
            users = new ArrayList<>();
            users.add(new User("aaa","111",1000));
            users.add(new User("bbb","222",2000));
            users.add(new User("ccc","333",3000));
            users.add(new User("ddd","444",4000));
            users.add(new User("eee","555",5000));
            fileUtil.writeObject(users,FILE_PATH);
        }
    }

    public ArrayList getUsers() {
        return users;
    }

    public void setUsers(ArrayList users) {
        this.users = users;
    }

    public User getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }

    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public String getFILE_PATH() {
        return FILE_PATH;
    }

    public void setFILE_PATH(String FILE_PATH) {
        this.FILE_PATH = FILE_PATH;
    }


    public void showPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }
    public static void main(String[] args) {
        new ATM05();
    }
}
